package com.sumfun.blog.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sumfun.blog.annotation.RequireLogin;
import com.sumfun.blog.common.BaseResponse;
import com.sumfun.blog.common.ErrorCode;
import com.sumfun.blog.common.PageRequest;
import com.sumfun.blog.common.ResultUtils;
import com.sumfun.blog.exception.BusinessException;
import com.sumfun.blog.model.domain.Tag;
import com.sumfun.blog.model.request.TagAddRequest;
import com.sumfun.blog.service.TagService;
import io.swagger.annotations.Api;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 标签Controller
 *
 * @author sumfun
 * @date 2024-04-21
 */
@Api(value = "标签信息Controller", tags = {"标签信息"})
@RestController
@RequestMapping("/tag")
public class TagController {

    @Resource
    private TagService tagService;

    @RequireLogin
    @PostMapping("/add")
    public BaseResponse<Boolean> addTag(@RequestBody TagAddRequest tagAddRequest, HttpServletRequest request){
        //判空
        if(tagAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag tag = new Tag();
        BeanUtils.copyProperties(tagAddRequest, tag);
        boolean tagId = tagService.save(tag);
        return ResultUtils.success(tagId);
    }

    @RequireLogin
    @DeleteMapping("/{id}")
    public BaseResponse<Boolean> deleteTag(@PathVariable long id) {
        if(id < 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = tagService.removeById(id);
        return ResultUtils.success(res);
    }

    @RequireLogin
    @PostMapping("/update")
    public BaseResponse<Boolean> updateTag(@RequestBody Tag tagUpdateRequest, HttpServletRequest request) {
        if(tagUpdateRequest == null || tagUpdateRequest.getTagId() == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        boolean res = tagService.updateById(tagUpdateRequest);
        return ResultUtils.success(res);
    }

    @GetMapping("/get")
    public BaseResponse<Tag> getTagById(long id) {
        if(id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Tag tag = tagService.getById(id);
        return ResultUtils.success(tag);
    }

    @GetMapping("/list")
    public BaseResponse<List<Tag>> getTagList() {
        List<Tag> tagList;
        tagList = tagService.list();
        return ResultUtils.success(tagList);
    }
    //
    @GetMapping("/list/page")
    public BaseResponse<Page> getTagListByPage(PageRequest tagQueryRequest) {
        Page<Tag> page = new Page(tagQueryRequest.getPageNum(), tagQueryRequest.getPageSize());
        Page<Tag> page1 = tagService.page(page);
        return ResultUtils.success(page1);
    }
}
